/*
 * @Description:
 * @Author: chenju
 * @Date: 2021-01-12 14:21:41
 * @LastEditors: chenju
 * @LastEditTime: 2022-12-02 17:21:52
 */
/**
 * @Description: 保存登录信息至localStorage
 * @param {*} data
 * @return {*}
 */
const saveLoginInfo = (data: any) => {
  const token = data.access_token;
  const refreshToken = data.refresh_token;
  const nowTime = new Date().getTime();
  // 过期时间
  const expiresIn = nowTime + data.expires_in * 1000;

  localStorage.setItem('token', token);
  localStorage.setItem('refresh_token', refreshToken);
  localStorage.setItem('expires_in', expiresIn.toString());
};

/**
 * @Description: 清空localStorage中的登录信息
 * @param {*}
 * @return {*}
 */
const clearLoginInfo = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('refresh_token');
  localStorage.removeItem('expires_in');
};

/**
 * @Description: 保存用户信息
 * @param {any} userInfo
 * @return {*}
 */
const saveUserInfo = (userInfo: any) => {
  localStorage.setItem('user_info', JSON.stringify(userInfo));
};

/**
 * @Description: 清除用户信息
 * @return {*}
 */
const clearUserInfo = () => {
  localStorage.removeItem('user_info');
};

export default {
  saveLoginInfo,
  clearLoginInfo,
  saveUserInfo,
  clearUserInfo
};
